//
//  FoodsTableViewController.m
//  B
//
//  Created by lanou on 16/5/14.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "FoodsTableViewController.h"
#import "FoodsModel.h"
#import "FoodsTableViewCell.h"
#import "FoodsWebViewController.h"

@interface FoodsTableViewController ()
@property (nonatomic, strong) NSMutableArray *listArrs;
@property (nonatomic, assign) NSInteger count;
@end

@implementation FoodsTableViewController

- (NSMutableArray *)listArrs
{
    if (!_listArrs) {
        _listArrs = [NSMutableArray array];
    }
    return _listArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"减肥食谱";
    self.count = 0;
    
    [self requestData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
}

- (void)footerRereshing
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:[NSString stringWithFormat:@"http://m.douguo.com/ajax/formore/recipelist/0/%ld", (long)self.count] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSArray *array = responseObject[@"data"][@"cooklist"];
        if (array.count > 0) {
            for (NSDictionary *dict in array) {
                FoodsModel *mode = [[FoodsModel alloc] init];
                [mode setValuesForKeysWithDictionary:dict];
                [self.listArrs addObject:mode];

            }
            self.count += 15;
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        } else {
            NSLog(@"11111");
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_footer removeFromSuperview];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}


- (void)requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://m.douguo.com/ajax/formore/recipelist/0/0" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"data"][@"cooklist"];
        for (NSDictionary *dic in array) {
            FoodsModel *mode = [[FoodsModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArrs addObject:mode];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArrs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FoodsModel *model = self.listArrs[indexPath.row];
    cell.cook_nameLable.text = model.cook_name;
    cell.nicknameLable.text = model.nickname;
    cell.contentLable.text = [NSString stringWithFormat:@"%@ 人 收藏     %@ 人 做过", model.collectsnum, model.dish_total];
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.cook_image]];
    // Configure the cell...
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodsWebViewController *webView = [[FoodsWebViewController alloc] init];
    FoodsModel *model = self.listArrs[indexPath.row];
    webView.string = model.cook_url;
    [self.navigationController pushViewController:webView animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
